<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * FormativeLab
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package		FormativeLab
 * @author		FormativeLab Dev Team
 * @copyright	Copyright (c) 2011 - 2013, FormativeLab, Inc.
 * @license		http://www.formativesoft.com/license/
 * @link		http://www.formativesoft.com/
 * @since		Version 2.0
 * @filesource ./application/helpers/rss_helper.php
 */

// ------------------------------------------------------------------------

/**
 * FormativeLab DOM Helpers
 *
 * @package		FormativeLab
 * @subpackage	Helpers
 * @category	Helpers
 * @author		FormativeLab Dev Team
 * @link		http://www.formativesoft.com/
 */
 
 // ------------------------------------------------------------------------

/**
 * RSSFeed class for generating RSS Feed dynamically. Its a Standard Entity
 * class swapping feed data, constant, static variable, methods
 *
 * RSSFeed class with contains an constructor with three required parameters.
 * This class will be helpfull for generating RSS Feed
 *
 * @access		public
 * @property	name
 * @property	mapping
 * @property	type
 */
 
if(!class_exists('RSSFeed')){
	class RSSFeed {
	
		public $channel;
		
		const VERSION 		= "2.0";
		const XMLNS_DC		= "http://purl.org/dc/elements/1.1/";
		const XMLNS_SY		= "http://purl.org/rss/1.0/modules/syndication/";
		const XMLNS_ADMIN	= "http://webns.net/mvcb/";
		const XMLNS_RDF		= "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
		const XMLNS_CONTENT = "http://purl.org/rss/1.0/modules/content/"; 

		const RSS_CHANNEL	= 'channel';
		const RSS_ITEM 		= 'item';
		const RSS			= 'rss';
		
		public function __construct(RSSChannel $channel){
			$this->channel	= $channel;
		}

		public function renderFeed(){
			renderRSSFeedAsRSS($this);
		}
	} 
 }
 
 
 // ------------------------------------------------------------------------

/**
 * RSSChannel class for generating RSS Feed dynamically. Its a Standard Entity
 * class swapping feed data, constant, static variable, methods
 *
 * RSSChannel class with contains an constructor with three required parameters.
 * This class will be helpfull for generating RSS Feed
 *
 * @access		public
 * @property	name
 * @property	mapping
 * @property	type
 */
 
if(!class_exists('RSSChannel')){
	class RSSChannel {
	
		public $title;
		public $feedLink;
		public $description;
		public $language;  
        public $creator;
		public $rights;
		public $resource;
		public $items;
		
		private $_CI;
	
		const RSS_CHANNEL	= RSSFeed::RSS_CHANNEL;		
		
		static $helper 		= array('xml', 'url', 'text');
		static $library		= array();
		
		public function __construct($title, $feedAction, $description, $creator, Array $items, $language = 'en-en', $resource = null){
			$this->init();
			$this->title			= $title;
			$this->feedLink			= site_url($feedAction);
			$this->description		= $description;
			$this->creator			= $creator;
			$this->language			= $language;
			$this->rights			= sprintf("Copyright %s.All rights reserved.", gmdate("Y", time()));
			$this->resource			= $resource ? $resource : "http://www.formativesoft.com/";
			$this->items			= $items;
		}
		
		private function init(){
			$this->_CI = &get_instance();		
			$this->_CI->load->helper(self::$helper);
			$this->_CI->load->library(self::$library);
		}
		
	} 
 }
 
 // ------------------------------------------------------------------------

/**
 * RSSItem class for generating RSS Feed dynamically. Its a Standard Entity
 * class swapping feed data, constant, static variable, methods
 *
 * RSSItem class with contains an constructor with three required parameters.
 * This class will be helpfull for generating RSS Feed
 *
 * @access		public
 * @property	name
 * @property	mapping
 * @property	type
 */
 
if(!class_exists('RSSItem')){
	class RSSItem {
	
		public $link;
		public $guid;
		public $description;
		public $pubdate; 

		private $_CI;
	
		const RSS_ITEM 		= RSSFeed::RSS_ITEM;	
		
		static $helper 		= array('xml', 'url', 'text');
		static $library		= array();
		
		public function __construct($linkAction, $guidAction, $description, DateTime $pubdate = null){
			$this->init();		
			$this->link			= site_url($linkAction);
			$this->guid			= site_url($guidAction);
			$this->description	= $description;
			$pubdate			= $pubdate? $pubdate : new DateTime();
			$this->pubdate		= $pubdate->format('D, d M Y H:i:s O');
		}
		
		private function init(){
			$this->_CI = &get_instance();		
			$this->_CI->load->helper(self::$helper);
			$this->_CI->load->library(self::$library);
		}
		
	} 
 }

 // ------------------------------------------------------------------------

/**
 * createRSSDOMDocument() - Contains an required input parameters
 * Its return an DOMDocument for generating rss response
 *
 * @access	public
 * @param	rssFeed
 * @return  DOMDocument
 */
if (!function_exists('createRSSDOMDocument')){
	function createRSSDOMDocument(RSSFeed $rssFeed){
		$dom				 		= new DOMDocument('1.0', 'UTF-8');
		$dom->standalone			= true;
		$dom->formatOutput 			= true;
		$dom->preserveWhiteSpace 	= false;
				
		$rss 	= &createRootElement($dom, RSSFeed::RSS);
		
		createAttribute($dom, $rss, 'version', 		 RSSFeed::VERSION);
		createAttribute($dom, $rss, 'xmlns:dc', 	 RSSFeed::XMLNS_DC);
		createAttribute($dom, $rss, 'xmlns:sy', 	 RSSFeed::XMLNS_SY);
		createAttribute($dom, $rss, 'xmlns:admin',	 RSSFeed::XMLNS_ADMIN);
		createAttribute($dom, $rss, 'xmlns:rdf', 	 RSSFeed::XMLNS_RDF);
		createAttribute($dom, $rss, 'xmlns:content', RSSFeed::XMLNS_CONTENT);
	
		$channel = &appendChild($dom, $rss, RSSFeed::RSS_CHANNEL);
		
		appendChild($dom, $channel, 'title', 		$rssFeed->channel->title);
		appendChild($dom, $channel, 'link', 		$rssFeed->channel->feedLink);
		appendChild($dom, $channel, 'description', 	$rssFeed->channel->description);
		
		appendChild($dom, $channel, 'dc:language', $rssFeed->channel->language);
		appendChild($dom, $channel, 'dc:creator',  $rssFeed->channel->creator);
		appendChild($dom, $channel, 'dc:rights',   $rssFeed->channel->rights);
		
		$admin		= appendChild($dom, $channel,'admin:generatoragent');		
		createAttribute($dom, $admin, 'rdf:resource', $rssFeed->channel->resource);
				
		foreach($rssFeed->channel->items as $key => $value){
			$item		= appendChild($dom, $admin, RSSFeed::RSS_ITEM);
			appendChild($dom, $item, 'link', 		$value->link);
			appendChild($dom, $item, 'guid', 		$value->guid);						
			$desc	= &appendChild($dom, $item, 'description');
			$text	= character_limiter($value->description, 200);
			$cdata	= $desc->ownerDocument->createCDATASection($text);
			$desc->appendChild($cdata);			
			appendChild($dom, $item, 'pubdate', 	$value->pubdate);			
		}		
        return $dom;
	}
}

// ------------------------------------------------------------------------

/**
 * renderDOMAsRSS() use for xml response
 *
 * @access	public
 * @param   dom
 * @param   format
 * @param   status
 * @return	void
 */
if (!function_exists('renderDOMAsRSS')){
	function renderDOMAsRSS(DOMDocument &$dom, $format = true, $status = 200){
		$dom->formatOutput 	= $format;
		$controller 		= &get_instance();
		
		$controller->output->set_status_header($status);
		$controller->output->set_header('Content-Type: application/rss+xml; charset=utf-8');
		$controller->output->set_output($dom->saveXML());
	}
}

// ------------------------------------------------------------------------

/**
 * renderRSSFeedAsRSS() use for xml response
 *
 * @access	public
 * @param   rssFeed
 * @param   format
 * @param   status
 * @return	void
 */
if (!function_exists('renderRSSFeedAsRSS')){
	function renderRSSFeedAsRSS(RSSFeed $rssFeed, $format = true, $status = 200){
		$dom	= createRSSDOMDocument($rssFeed);
		renderDOMAsRSS($dom, $format, $status);
	}
}


/* End of file rss_helper.php */
/* Location: ./application/helpers/rss_helper.php */